﻿using MaterialDesignThemes.Wpf;
using Prism.Commands;
using StarryEdge.Core.Enums;
using StarryEdge.Core.Models.Interact;
using StarryEdge.Core.Parameter;
using StarryEdge.Parameter;
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace StarryEdge.Menu.SettingSystem.ViewModels.Dialog
{
    public class NewScanSignalPopupDialog<T> : Prism.Mvvm.BindableBase
    {
        public IEnumerable<DeviceTypes> DeviceList
        {
            get
            {
                foreach (DeviceTypes c in (DeviceTypes[])Enum.GetValues(typeof(DeviceTypes)))
                {
                    yield return c;
                }
            }
        }
        public IEnumerable<int> DeviceIndexList
        {
            get
            {
                for (int i = 1; i < 20; i++)
                {
                    yield return i;
                }
            }
        }
        private bool _isUpdate = false;
        private System.Windows.Visibility _sUpdate;

        public System.Windows.Visibility IsUpdate
        {
            get { return _sUpdate; }
            set { SetProperty(ref _sUpdate, value); }
        }

        public ScanSignalModel<T> IAM { get; set; }
        private SystemConfigParameter _configParameter;
        private ObservableCollection<ScanSignalModel<T>> _models;
        public NewScanSignalPopupDialog(ObservableCollection<ScanSignalModel<T>> models, ISystemConfigParameter systemConfig, ScanSignalModel<T> scanSignalModel, bool isUpdate = false)
        {
            _models = models;
            _configParameter = (SystemConfigParameter)systemConfig;
            _isUpdate = isUpdate;
            if (_isUpdate)
            {
                IAM = scanSignalModel;
                IsUpdate = System.Windows.Visibility.Visible;
            }
            else
            {
                IsUpdate = System.Windows.Visibility.Collapsed;
                IAM = new ScanSignalModel<T>() { Index = models.Count + 1 };
            }
        }

        public DelegateCommand Confirm => new DelegateCommand(() =>
        {
            if (string.IsNullOrEmpty(IAM.Name))
            {
                if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
                return;
            }
            if (!_isUpdate)
            {
                Guid guid = Guid.NewGuid();
                _models.Add(new ScanSignalModel<T>()
                {
                    Address_Start = IAM.Address_Start,
                    Length_Resection = IAM.Length_Resection,
                    Length_Signal = IAM.Length_Signal,
                    DeviceType = IAM.DeviceType,
                    Name = IAM.Name,
                    Heartbeat = IAM.Heartbeat,
                    AddressingMethod = IAM.AddressingMethod,
                });
            }
            _configParameter.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });

        public DelegateCommand Delete => new DelegateCommand(() =>
        {
            _models.Remove(IAM);
            _configParameter.Save();
            if (DialogHost.IsDialogOpen(PopUpTypes.Global.ToString())) { DialogHost.Close(PopUpTypes.Global.ToString()); }
        });

    }
}
